let app = getApp();
import {list as productList} from '../../../api/product'
import {list as goodsCategroyList} from '../../../api/goodsCategroy'
const util = require('../../../utils/util.js')
Page({
      data: {
          goodsCategoryId:'',
          classifyList: [],
          goodsList: [ ],
          supplierId:'',
          keyword:''
      },
    onLoad(options) {
        this.data.supplierId = options.supplierId
        this.setData({
          supplierId:this.data.supplierId 
        })
      },
    onShow(){
        this.getGoodsCategroyList()
    },
    handleQueryClear(){
        this.data.keyword = ""
        this.setData({
            keyword:this.data.keyword,
        })
        this.getProductList()
    },
    getGoodsCategroyList(){
    let params ={

    }
    goodsCategroyList(params).then(res=>{
        this.data.classifyList = res.data.data
        this.data.goodsCategoryId = this.data.classifyList[0].id
        this.setData({
            classifyList:this.data.classifyList,
            goodsCategoryId:this.data.goodsCategoryId
        })
        this.getProductList()

    })
  },
    getProductList(){
        let params ={
            customerId:this.data.supplierId,
            goodsCategoryId:this.data.goodsCategoryId,
        }
        productList(params).then(res=>{
          this.data.goodsList = res.data.data
          this.setData({
            goodsList:this.data.goodsList
          })
        })
    },
    classifyTab(e){
        this.data.tabCur = e.currentTarget.dataset.index
        this.data.goodsCategoryId = this.data.classifyList[e.currentTarget.dataset.index].id
          this.setData({
              tabCur:this.data.tabCur,
              goodsCategoryId:this.data.goodsCategoryId
          })
        this.getProductList()
    },
    keywordUpdate(e){
        this.data.keyword = e.detail.value
        this.setData({
            keyword: this.data.keyword
        })
    },
    handleQuery(){
          this.getProductListSearch()
    },
    getProductListSearch(){
        let params ={
            customerId:this.data.supplierId,
            keyword:this.data.keyword
        }
        productList(params).then(res=>{
            this.data.goodsList = res.data.data
            this.setData({
                goodsList:this.data.goodsList
            })
        })
    },
    goodsListClick(e){
        let pages = getCurrentPages();
        let prevPage = pages[pages.length-2];
        prevPage.calculation();
        prevPage.setData({
            tradeName: e.currentTarget.dataset.item.name,
            productId: e.currentTarget.dataset.item.id,
            price: e.currentTarget.dataset.item.price,
            minimum: e.currentTarget.dataset.item.minimum
        });
        wx.navigateBack({
            delta: 1,
        })
    },
    newProducts(){
        wx.navigateTo({
            url: '/pages/product/product',
        })
    }
})